Match and Regular Expressions

Hi All

Coming into some issues with Match and regular expressions.

In an attribute are the possibles values:

Block 10 (G70, K90, LKJ)
Block 10 (LKJ, Not K90)

Not all of these will be present, but I need to know how to discriminate between partial strings and whole matches i.e.

I want to count those with G70, or K90 present, but NOT count 'Not K90'. So the first example would be counted on the grounds that G70 and K90 are present.

The second example I don't want to count as it only has 'Not K90', but currently my match expression sees the K90 and counts it.

Does anyone have any ideas please?

Many thanks
Roobroo - Tue Jun 15 10:33:20 EDT 2010

Re: Match and Regular Expressions
llandale - Tue Jun 15 16:30:55 EDT 2010

I'm certainly clumsy with regular expressions and hopefully someone else will chime in here, but I note the following:
{code]
Regexp re = regexp2(".*(Not K90)|(K90)")
void Test(string s)
{ if (re s) //
then print "yes\t[" s[match 1] "]\t[" s[match 2] "]\t*" s "*\n"
else print "no \t*" s "*\n"
}

Test("yyy K90yyy")
Test("yyy Not K90")
Test("yyy NotK90")
{code}
prints this:
yes [] [K90] *yyy K90yyy*
yes [Not K90] [] *yyy Not K90*
yes [] [K90] *yyy NotK90*

So you could perhaps check for some kind of match, and when you find one figure out if its a match for [match 1] or [match 2], and go from there.

  • Louie

Re: Match and Regular Expressions
Mathias Mamsch - Tue Jun 15 17:44:54 EDT 2010

As Louie said, using two matches and then checking them before counting is probably the best solution. Since regexp does not support lookahead, you can only specify very awkward regular expressions that will count K90 but not count NOT K90. Its possible but awkward.

Regards, Mathias
 

Regexp re = regexp "([^Tt ] *K90)|([^Oo][tT] ?K90)|([^Nn][oO][tT ] *K90)"
 
void Test(string s)
{ 
  if (re s) 
    print "yes\t[" s "]\n"
  else 
        print "no \t*" s "*\n"
 
  // for i in 0:3 do print s[match i] "\n"
}
 
Test("yyy K90yyy")
Test("yyy Not K90")
Test("yyy NotK90")
Test("yyyNotK90")
Test("yyyNot yK90")

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Match and Regular Expressions
Roobroo - Wed Jun 16 04:23:07 EDT 2010

Mathias Mamsch - Tue Jun 15 17:44:54 EDT 2010

As Louie said, using two matches and then checking them before counting is probably the best solution. Since regexp does not support lookahead, you can only specify very awkward regular expressions that will count K90 but not count NOT K90. Its possible but awkward.

Regards, Mathias
 

Regexp re = regexp "([^Tt ] *K90)|([^Oo][tT] ?K90)|([^Nn][oO][tT ] *K90)"
 
void Test(string s)
{ 
  if (re s) 
    print "yes\t[" s "]\n"
  else 
        print "no \t*" s "*\n"
 
  // for i in 0:3 do print s[match i] "\n"
}
 
Test("yyy K90yyy")
Test("yyy Not K90")
Test("yyy NotK90")
Test("yyyNotK90")
Test("yyyNot yK90")

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Louie, Mathias

Thank you so much for your responses.

I believe that you have found a workable solution for me.

Thanks again.

Re: Match and Regular Expressions
Bosch - Fri Jul 22 05:46:28 EDT 2011

Mathias Mamsch - Tue Jun 15 17:44:54 EDT 2010

As Louie said, using two matches and then checking them before counting is probably the best solution. Since regexp does not support lookahead, you can only specify very awkward regular expressions that will count K90 but not count NOT K90. Its possible but awkward.

Regards, Mathias
 

Regexp re = regexp "([^Tt ] *K90)|([^Oo][tT] ?K90)|([^Nn][oO][tT ] *K90)"
 
void Test(string s)
{ 
  if (re s) 
    print "yes\t[" s "]\n"
  else 
        print "no \t*" s "*\n"
 
  // for i in 0:3 do print s[match i] "\n"
}
 
Test("yyy K90yyy")
Test("yyy Not K90")
Test("yyy NotK90")
Test("yyyNotK90")
Test("yyyNot yK90")

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Hello,

i have one string "s_Projectname" by value "ProductHMINavServerLib" and another list of strings which i had put into a skip "sk" those are

1. AICFHMINavServerLibRM
2. AICFHMINavServerLibPM
3. AICFHMINavServerLibSM
4. AICFHMINavServerLibFO
5. AICFHMINavServerLibST

so what i am doing is
for s_name in sk do
{
if(matches("."s_Projectname".", s_name)
{
print s_name"\n"
}
}

as i am not able to match the strings using pattern matching in dxl.

actually the project name is not intialized in the code,it is the project name which i am getting it from GUI,so the project name and the values in the skip will be varying , the above what i have mentioned is just an example if there are strings like that how can we macth it.

kindly do the need full.

if not possible kindly reply back.

Thank you!

regards,
Shruthi

Re: Match and Regular Expressions
Mathias Mamsch - Fri Jul 22 06:47:07 EDT 2011

Bosch - Fri Jul 22 05:46:28 EDT 2011
Hello,

i have one string "s_Projectname" by value "ProductHMINavServerLib" and another list of strings which i had put into a skip "sk" those are

1. AICFHMINavServerLibRM
2. AICFHMINavServerLibPM
3. AICFHMINavServerLibSM
4. AICFHMINavServerLibFO
5. AICFHMINavServerLibST

so what i am doing is
for s_name in sk do
{
if(matches("."s_Projectname".", s_name)
{
print s_name"\n"
}
}

as i am not able to match the strings using pattern matching in dxl.

actually the project name is not intialized in the code,it is the project name which i am getting it from GUI,so the project name and the values in the skip will be varying , the above what i have mentioned is just an example if there are strings like that how can we macth it.

kindly do the need full.

if not possible kindly reply back.

Thank you!

regards,
Shruthi

You could search for the project name simply by using findPlaintext or just build a regular expression from your projectName e.g.
 

// make sure there are no special characters in your project name if you use regexp 
Regexp rePrj = regexp "(.*)" sProjectName "(.*)"
 
for s_name in sk do {
    if(rePrj s_name) {
       print "Match: " s_name "\n"
       string sPrefix = s_name[match 1]
       string sPostfix = s_name[match 2]
       print "Prefix: " sPrefix " Postfix:" sPostfix "\n"
    }
}
delete rePrj // only in DOORS 9.1+

 


maybe that helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Match and Regular Expressions
Bosch - Fri Jul 22 09:16:11 EDT 2011

Mathias Mamsch - Fri Jul 22 06:47:07 EDT 2011

You could search for the project name simply by using findPlaintext or just build a regular expression from your projectName e.g.
 

// make sure there are no special characters in your project name if you use regexp 
Regexp rePrj = regexp "(.*)" sProjectName "(.*)"
 
for s_name in sk do {
    if(rePrj s_name) {
       print "Match: " s_name "\n"
       string sPrefix = s_name[match 1]
       string sPostfix = s_name[match 2]
       print "Prefix: " sPrefix " Postfix:" sPostfix "\n"
    }
}
delete rePrj // only in DOORS 9.1+

 


maybe that helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Hello Mathias,

Thanks for your quick response!

i have tried with the same string which i had mentioned in the above its not printing only.
its not even entering into the loop.

kindly help me..

Thank you!

Regards,
Shruthi

Re: Match and Regular Expressions
llandale - Fri Jul 22 14:38:39 EDT 2011

Mathias Mamsch - Fri Jul 22 06:47:07 EDT 2011

You could search for the project name simply by using findPlaintext or just build a regular expression from your projectName e.g.
 

// make sure there are no special characters in your project name if you use regexp 
Regexp rePrj = regexp "(.*)" sProjectName "(.*)"
 
for s_name in sk do {
    if(rePrj s_name) {
       print "Match: " s_name "\n"
       string sPrefix = s_name[match 1]
       string sPostfix = s_name[match 2]
       print "Prefix: " sPrefix " Postfix:" sPostfix "\n"
    }
}
delete rePrj // only in DOORS 9.1+

 


maybe that helps, regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

This complete code works:

// make sure there are no special characters in your project name if you use regexp 
 
string sProjectName = "MyProject"
string sLongName = "Prefix-" sProjectName "-Suffix"
string s_name
 
Skip   skp = createString()
put(skp, sLongName, sLongName)        // includes prefix and suffix
put(skp, sProjectName, sProjectName)  // exact name
 
 
Regexp rePrj = regexp "(.*)" sProjectName "(.*)"
 
for s_name in skp do {
    if(rePrj s_name) {
       print "Match: Found [" sProjectName "] in string [" s_name "]\n"
       string sPrefix  = s_name[match 1]
       string sPostfix = s_name[match 2]
       print "Prefix: [" sPrefix "] Postfix:[" sPostfix "]\n"
    }
}
delete rePrj // only in DOORS 9.1+

 

  • Louie

HA! Nice to know someone could make the same mistake I did! Of course, you have a good excuse, English being the 2nd language.

When I started out I did a lot of string manipulation and wrote and used functions for "fPreFix" and "fPostFix", used by a dozen programs. Ended up renaming them "fPrefix" and "fPostfix". After recieving redicule from "friends" pointing out that "Postfix" is not a word but "Suffix" is, I renamed that function to "fSuffix". Also searched out all my "Postfix" variables and renamed them also. (Also noting "Prefix" and "Suffix" have the same number of characters, making reports etc line up better)

 

Re: Match and Regular Expressions
Mathias Mamsch - Tue Jul 26 05:43:18 EDT 2011

llandale - Fri Jul 22 14:38:39 EDT 2011

This complete code works:

// make sure there are no special characters in your project name if you use regexp 
 
string sProjectName = "MyProject"
string sLongName = "Prefix-" sProjectName "-Suffix"
string s_name
 
Skip   skp = createString()
put(skp, sLongName, sLongName)        // includes prefix and suffix
put(skp, sProjectName, sProjectName)  // exact name
 
 
Regexp rePrj = regexp "(.*)" sProjectName "(.*)"
 
for s_name in skp do {
    if(rePrj s_name) {
       print "Match: Found [" sProjectName "] in string [" s_name "]\n"
       string sPrefix  = s_name[match 1]
       string sPostfix = s_name[match 2]
       print "Prefix: [" sPrefix "] Postfix:[" sPostfix "]\n"
    }
}
delete rePrj // only in DOORS 9.1+

 

  • Louie

HA! Nice to know someone could make the same mistake I did! Of course, you have a good excuse, English being the 2nd language.

When I started out I did a lot of string manipulation and wrote and used functions for "fPreFix" and "fPostFix", used by a dozen programs. Ended up renaming them "fPrefix" and "fPostfix". After recieving redicule from "friends" pointing out that "Postfix" is not a word but "Suffix" is, I renamed that function to "fSuffix". Also searched out all my "Postfix" variables and renamed them also. (Also noting "Prefix" and "Suffix" have the same number of characters, making reports etc line up better)

 

"Postfix" is used in Math or Information science as an alternative to Suffix. See for example here: http://en.wikipedia.org/wiki/Suffix ... So no need to change yours scripts. Just tell them you are a programmer gg Mathias

Re: Match and Regular Expressions
Bosch - Tue Oct 11 02:29:12 EDT 2011

Mathias Mamsch - Tue Jul 26 05:43:18 EDT 2011
"Postfix" is used in Math or Information science as an alternative to Suffix. See for example here: http://en.wikipedia.org/wiki/Suffix ... So no need to change yours scripts. Just tell them you are a programmer gg Mathias

Hello,

May i know how to match multiple lines in a log file.
Attached is the log file from where i have to get the text between two tags #IssueStart# and #IssueEnd#.
Kindly help me out how to get the multiple lines.

Thank you!

Regards,
Shruthi
Attachments

attachment_14691584_JIRA_TO_DOORS_Info.txt

Re: Match and Regular Expressions
llandale - Tue Oct 11 10:33:46 EDT 2011

Bosch - Tue Oct 11 02:29:12 EDT 2011
Hello,

May i know how to match multiple lines in a log file.
Attached is the log file from where i have to get the text between two tags #IssueStart# and #IssueEnd#.
Kindly help me out how to get the multiple lines.

Thank you!

Regards,
Shruthi

This is not a regular expression problem. Seems to me you want to read each line in the file, then check the characteristics of that line, and take appropriate action. Here is some psuedo code

Stream in = read(file)
Buffer bufIssue = create()
bool   IssueMode = false
string Line, NameProject = "", NameFilter = ""
while (not end of stream)
{  in >> Line   // read the next line
   if     (Line == "#IssueEnd#")
   {  IssueMode   = false
      print "===Issue Block ===\n" tempStringOf(bufIssue) "\n=== End Block ===\n"
      bufIssue    = ""
   }
   elseif (IssueMode) bufIssue += Line "\n"
   elseif (Line == "#IssueStart#")      IssueMode   = true
   elseif (Line[0:10] == "ProjectName") NameProject = Line[12:]
   elseif (Line[0: 5] == "Filter"       NameFilter  = Line[7:]
   else {} //poorly formed line
}
close(in)
delete(bufIssue)
if (IssueMode) print "Poorly formed file, not end-of-block command '#IssueEnd#'\n"
print "NameProject:\t" NameProject\nNameFilter  \t" NameFilter "\n"

Re: Match and Regular Expressions
Bosch - Wed Oct 12 00:28:16 EDT 2011

llandale - Tue Oct 11 10:33:46 EDT 2011

This is not a regular expression problem. Seems to me you want to read each line in the file, then check the characteristics of that line, and take appropriate action. Here is some psuedo code

Stream in = read(file)
Buffer bufIssue = create()
bool   IssueMode = false
string Line, NameProject = "", NameFilter = ""
while (not end of stream)
{  in >> Line   // read the next line
   if     (Line == "#IssueEnd#")
   {  IssueMode   = false
      print "===Issue Block ===\n" tempStringOf(bufIssue) "\n=== End Block ===\n"
      bufIssue    = ""
   }
   elseif (IssueMode) bufIssue += Line "\n"
   elseif (Line == "#IssueStart#")      IssueMode   = true
   elseif (Line[0:10] == "ProjectName") NameProject = Line[12:]
   elseif (Line[0: 5] == "Filter"       NameFilter  = Line[7:]
   else {} //poorly formed line
}
close(in)
delete(bufIssue)
if (IssueMode) print "Poorly formed file, not end-of-block command '#IssueEnd#'\n"
print "NameProject:\t" NameProject\nNameFilter  \t" NameFilter "\n"

Thank you so much,this is what i needed.

similarly may i know how to read each line within issue block and end block,like i want to get the DOORSID,object heading ,object text, and JIRAURL with respective data.

Thank you!

Regards,
Shruthi

Re: Match and Regular Expressions
llandale - Wed Oct 12 15:12:20 EDT 2011

Bosch - Wed Oct 12 00:28:16 EDT 2011
Thank you so much,this is what i needed.

similarly may i know how to read each line within issue block and end block,like i want to get the DOORSID,object heading ,object text, and JIRAURL with respective data.

Thank you!

Regards,
Shruthi

Someone will surely jump in here with an elegant Regular Expression solution, but...

string StripStartEnd(string in_Line)
{  string Result = in_Line
   if (Result[0:6] == "#Start#") Result = Result[7:]
   int len = length(Result)
   if (Result[len-5:] == "#End#)   Result = Result[0:len-6]
   return(Result)
}
 
void  ProcessIssueLine(string in_Line, &out_DoorsID, &out_Heading, &out_Text, &out_Jirautl)
{
   if    (in_Line[0: 7] == "DOORSID=")        out_DoorsID = In_Line[8:]
   elseif(in_Line[0:14] == "Object Heading=") out_Heading = StripStartEnd(in_Line[15:])
   elseif(in_Line[0:11] == "Object Text=")    out_Text    = StripStartEnd(in_Line[12:])
   elseif(in_Line[0: 7] == "JIRAURL=")        out_Jiraurl = InLine[8:]
}


In the main code:
o When IssueStart is detected: DoorsID = ""; Heading = ""; Text = ""; Jiraulr = ""
o While in IssueMode: ProcessIssueLine(Line, DoorsID, Heading, Text, Jiraurl)
o When IssueEnd detected: Do something with DoorsID, Heading, Text, and Jiraurl.

 

 

  • Louie